 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;
import java.sql.SQLException;
import java.util.Random;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author rj
 */
public class RandomGenerator {
    
    public static boolean checkAccountID(int value){
        boolean isValid = true;
        
        try {
            
     //       value = 3;
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            PreparedStatement stat = conn.prepareStatement("SELECT AccountID FROM user");
            ResultSet rs = stat.executeQuery();
            
            while(rs.next()){
                if(rs.getInt("AccountID") == value)
                    isValid = false;
                
            }
            
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(CheckForUnique.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isValid;
    }
    
    public static boolean checkTransactionID(int value){
        boolean isValid = true;
        
        try {
            
     //       value = 3;
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            PreparedStatement stat = conn.prepareStatement("SELECT TransactionID FROM transaction");
            ResultSet rs = stat.executeQuery();
            
            while(rs.next()){
                if(rs.getInt("TransactionID") == value)
                    isValid = false;
                
            }
            
            
            
        } catch (SQLException ex) {
            Logger.getLogger(CheckForUnique.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isValid;
    }
    
    public static boolean checkProductID(int value){
        boolean isValid = true;
        
        try {
            
     //       value = 3;
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            PreparedStatement stat = conn.prepareStatement("SELECT ProductID FROM product");
            ResultSet rs = stat.executeQuery();
            
            while(rs.next()){
                if(rs.getInt("ProductID") == value)
                    isValid = false;
                
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(CheckForUnique.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isValid;
    }
    
    
    public static boolean checkFakeProductID(int value){
        boolean isValid = true;
        
        try {
            
     //       value = 3;
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            PreparedStatement stat = conn.prepareStatement("SELECT FakeProductID FROM productidtrack");
            ResultSet rs = stat.executeQuery();
            
            while(rs.next()){
                if(rs.getInt("FakeProductID") == value)
                    isValid = false;
                
            }
            
            
            
        } catch (SQLException ex) {
            Logger.getLogger(CheckForUnique.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isValid;
    }
    
    public static boolean checkFakeAccountID(int value){
        boolean isValid = true;
        
        try {
            
     //       value = 3;
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            PreparedStatement stat = conn.prepareStatement("SELECT FakeAccountID FROM useridtrack");
            ResultSet rs = stat.executeQuery();
            
            while(rs.next()){
                if(rs.getInt("FakeAccountID") == value)
                    isValid = false;
                
            }
            
            conn.close();
            
        } catch (SQLException ex) {
            Logger.getLogger(CheckForUnique.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isValid;
    }
    
    
    public static boolean checkFakeTransactionID(int value){
        boolean isValid = true;
        
        try {
            
     //       value = 3;
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            PreparedStatement stat = conn.prepareStatement("SELECT FakeTransactionID FROM transidtrack");
            ResultSet rs = stat.executeQuery();
            
            while(rs.next()){
                if(rs.getInt("FakeTransactionID") == value)
                    isValid = false;
                
            }
            
            conn.close();
            
        } catch (SQLException ex) {
            Logger.getLogger(CheckForUnique.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isValid;
    }
    
    
   public int generateProductID(){
//    log("Generating random integers in the range 1..10.");
    
    int START = 1000;
    int END = 9999;
    Random random = new Random();
   
    int randomNum = showRandomInteger(START, END, random);
    
    while(checkProductID(randomNum) == false){
        randomNum = showRandomInteger(START, END, random);
    }
    
    return randomNum;
//    log("Done.");
  }

   public int generateTransactionID(){
//    log("Generating random integers in the range 1..10.");
     
    int START = 1000;
    int END = 9999;
    Random random = new Random();
    
    int randomNum = showRandomInteger(START, END, random);
    
    while(checkTransactionID(randomNum) == false){
        randomNum = showRandomInteger(START, END, random);
    }
    
    return randomNum;
//    log("Done.");
  }   
   
   public int generateAccountID(){
//    log("Generating random integers in the range 1..10.");
    
    
    int START = 1000;
    int END = 9999;
    Random random = new Random();
    
    int randomNum = showRandomInteger(START, END, random);
    
    while(checkAccountID(randomNum) == false){
        randomNum = showRandomInteger(START, END, random);
    }
    return randomNum;
//    log("Done.");
  }      
  
    public int generateFakeProductID(){
//    log("Generating random integers in the range 1..10.");
    
    int START = 1000;
    int END = 9999;
    Random random = new Random();
   
    int randomNum = showRandomInteger(START, END, random);
    
    while(checkFakeProductID(randomNum) == false){
        randomNum = showRandomInteger(START, END, random);
    }
    
    return randomNum;
//    log("Done.");
  }

   public int generateFakeAccountID(){
//    log("Generating random integers in the range 1..10.");
    
    
    int START = 1000;
    int END = 9999;
    Random random = new Random();
  
    int randomNum = showRandomInteger(START, END, random);
    
    while(checkFakeAccountID(randomNum) == false){
        randomNum = showRandomInteger(START, END, random);
    }
    
    return randomNum;
//    log("Done.");
  }     
   
   
   public int generateFakeTransactionID(){
//    log("Generating random integers in the range 1..10.");
    
    
    int START = 1000;
    int END = 9999;
    Random random = new Random();
  
    int randomNum = showRandomInteger(START, END, random);
    
    while(checkFakeTransactionID(randomNum) == false){
        randomNum = showRandomInteger(START, END, random);
    }
    
    return randomNum;
//    log("Done.");
  }  
   
  private int showRandomInteger(int aStart, int aEnd, Random aRandom){
    if ( aStart > aEnd ) {
      throw new IllegalArgumentException("Start cannot exceed End.");
    }
    //get the range, casting to long to avoid overflow problems
    long range = (long)aEnd - (long)aStart + 1;
    // compute a fraction of the range, 0 <= frac < range
    long fraction = (long)(range * aRandom.nextDouble());
    int randomNumber =  (int)(fraction + aStart);    
    
    return randomNumber;
//    log("Generated : " + randomNumber);
  }
  
  private static void log(String aMessage){
    System.out.println(aMessage);
  }
  
     public int generateTimeID(){
//    log("Generating random integers in the range 1..10.");
    
    
    int START = 1000;
    int END = 9999;
    Random random = new Random();
    
    int randomNum = showRandomInteger(START, END, random);
    
    while(checkTimeID(randomNum) == false){
        randomNum = showRandomInteger(START, END, random);
    }
    return randomNum;
//    log("Done.");
  }      

    public static boolean checkTimeID(int value){
        boolean isValid = true;
        
        try {
            
     //       value = 3;
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            PreparedStatement stat = conn.prepareStatement("SELECT TimeID FROM timelog");
            ResultSet rs = stat.executeQuery();
            
            while(rs.next()){
                if(rs.getInt("TimeID") == value)
                    isValid = false;
                
            }
            
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(CheckForUnique.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isValid;
    }
 
}
